package com.wing.purchase.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.product.service.DynamicService;
import com.wing.purchase.dao.PurchasePlanCheckDao;
import com.wing.purchase.model.entity.PurchasePlanCheck;
import com.wing.purchase.model.entity.PurchasePlanCheckDetail;
import com.wing.purchase.model.request.PurchasePlanCheckForm;
import com.wing.purchase.model.response.PurchasePlanCheckVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购计划核对-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class PurchasePlanCheckServiceImpl extends ServiceImpl<PurchasePlanCheckDao, PurchasePlanCheck> implements PurchasePlanCheckService {

    @Autowired
    private PurchasePlanDetailService purchasePlanDetailService;
    @Autowired
    private PurchasePlanCheckDetailService purchasePlanCheckDetailService;
    @Autowired
    private DynamicService dynamicService;

    @Override
    @Transactional(readOnly = false)
    public int add(PurchasePlanCheckForm purchasePlanCheckForm) {

        //校验

        PurchasePlanCheck purchasePlanCheck =new PurchasePlanCheck();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(PurchasePlanCheck.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(purchasePlanCheckForm, purchasePlanCheck, copyOptions);

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyyMMdd");
        String code = dynamicService.getMaxCode("receipts_no", "pur_purchase_plan_check");
        String day = simpleDateFormat.format(new Date());
        purchasePlanCheck.setHasGenerateOrder(false);
        purchasePlanCheck.setReceiptsNo("PC"+day+code);

        int i = baseMapper.insert(purchasePlanCheck);
        if( i > 0){
            for (PurchasePlanCheckDetail purchasePlanDetail: purchasePlanCheckForm.getCheckInfos()){
                purchasePlanDetail.setCheckId(purchasePlanCheck.getId());
                purchasePlanDetail.setProductDetailId(purchasePlanDetail.getProductDetailId());
                //剩余数量
                purchasePlanDetail.setSurplusQuantity(purchasePlanDetail.getCheckNumber());
            }
            purchasePlanCheckDetailService.saveBatch(purchasePlanCheckForm.getCheckInfos());
            //将核对中的领料计划转换为已核对  --  条件（紧急类型、计划单号）
            purchasePlanDetailService.changePlanCheckStatus(2,1, null, null);
        }
        return i;
    }

    @Override
    public List<PurchasePlanCheckVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<PurchasePlanCheckVO> pageList4app(Page page, Map<String, Object> params) {
        List<PurchasePlanCheckVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public PurchasePlanCheckVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<PurchasePlanCheckVO> findCheckInfoList(Map<String, Object> params) {
        int i = purchasePlanDetailService.changePlanCheckStatus(1, 0, null, null);
        return purchasePlanDetailService.findCheckInfoList(params);
    }
}
